home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / Base / test / sa / math < prev   
Text File  |  1996-07-13  |  5KB  |  123 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- test_math.sa: mathematical testing functions
  3. -- Author: Alex Cozzi <cozzi@neurop2.ruhr-uni-bochum.de>
  4. -- $Id: math_test.sa,v 1.3 1996/07/13 05:40:12 gomes Exp $
  5. --
  6. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  7. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  8. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  9. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  10. -------------------------------------------------------------------
  11.  
  12. class TEST_MATH{T < $REAL_NUMBER{T} } is
  13.    include TEST;
  14.    -- some basic tests on floating point classes
  15.  
  16.    basic(name:STR) is
  17.       class_name(name);
  18.       -------------------------------------------------------------
  19.       -- first obtain some constants
  20.       -- some normal ones, but we test them anyway,
  21.       -- maybe it a little paranoic, though.
  22.       zero:T :=#T(0.0);
  23.       test("0",zero.str,"0");
  24.       one:T :=#T(1.0);
  25.       test("1",one.str,"1");
  26.       ------------------------------------------------------------
  27.       -- now some strange beasts:
  28.       -- negative zero
  29.       -- this one fails, because RATs are used in the compiler
  30.       -- for floating point, and RATs don't have a signed zero.
  31.       unchecked_test("#T(-0.0)",(#T(-0.0)).str,"-0");
  32.       -- this one works
  33.       nzero:T :=-zero;
  34.       test("-0",nzero.str,"-0");
  35.       -- infinity
  36.       inf:T :=#T(1.0/0.0);
  37.       test("1.0/0.0",inf.str,"Inf");
  38.       -- negative infinity
  39.       ninf:T :=#T(-1.0/0.0);
  40.       test("-1.0/0.0",ninf.str,"-Inf");
  41.       ------------------------------------------------------------
  42.       -- an even more strange beast, a NaN, 
  43.       -- this gives me some funny problems.
  44.       -- Let's show something:
  45.       -- this first test is allright
  46.       test("0.0/0.0 with variables",(zero/zero).str,"NaN");
  47.       -- this expression is allright too.
  48.       -- Specialized the following functions due to hassles
  49.       -- with the typebound (ben)
  50.       nan:FLT :=FLT::zero/FLT::zero;
  51.       test("0.0/0.0",nan.str,"NaN");
  52.       nan2:FLTD :=FLTD::zero/FLTD::zero;
  53.       test("0.0/0.0",nan2.str,"NaN");
  54.       -- instead, this one dumps core
  55.       test("0.0/0.0 with constants. May dump core",(0.0/0.0).str,"NaN");
  56.       -- and this expression too
  57.       -- nan3:T :=#T(0.0/0.0); test("0.0/0.0",nan2.str,"NaN");
  58.       -----------------------------------------------------------
  59.       -- Now some fun, let's test equality
  60.       -- an easy one
  61.       test("1=1",(one=one).str,"true");
  62.       -- in IEEE -0 = +0
  63.       test("-0=0",(zero=nzero).str,"true");
  64.       -- let's test extreme cases
  65.       test("Inf=Inf",(inf=inf).str,"true");
  66.       test("-Inf=-Inf",(ninf=ninf).str,"true");
  67.       test("Inf=-Inf",(inf=ninf).str,"false");
  68.       -- in IEEE NaN /= NaN
  69.       test("NaN=NaN",(nan=nan).str,"false");
  70.       -----------------------------------------------------------
  71.       -- now let's test inequality
  72.       -- an easy one
  73.       test("1/=1",(one/=one).str,"false");
  74.       -- in IEEE -0 = +0
  75.       test("-0/=0",(zero/=nzero).str,"false");
  76.       -- let's test extreme cases
  77.       test("Inf/=Inf",(inf/=inf).str,"false");
  78.       test("-Inf/=-Inf",(ninf/=ninf).str,"false");
  79.       test("Inf/=-Inf",(inf/=ninf).str,"true");
  80.       -- in IEEE NaN /= NaN
  81.       test("NaN/=NaN",(nan/=nan).str,"true");
  82.       -----------------------------------------------------------
  83.       -- testing of IEEE classification functions: is_nan
  84.       -- All of these dump core
  85.       test("0.0.is_nan. May dump core",zero.is_nan.str,"false");
  86.       test("-0.0.is_nan. May dump core",nzero.is_nan.str,"false");
  87.       test("NaN.is_nan. May dump core",nan.is_nan.str,"true");
  88.       test("Inf.is_nan. May dump core",inf.is_nan.str,"false");
  89.       test("-Inf.is_nan. May dump core",ninf.is_nan.str,"false");
  90.       -----------------------------------------------------------
  91.       -- testing of math lib functions, sqrt
  92.       test("sqrt(0.0)",zero.sqrt.str,"0");
  93.       -- I'm not sure about the following,
  94.       test("sqrt(-0.0)",(-zero).sqrt.str,"-0");
  95.       test("sqrt(-1.0)",(-one).sqrt.str,"NaN");
  96.       ----------------------------------------------------------
  97.       -- testing of trascendental function, log
  98.       -- Solaris at least does the wrong thing by default,
  99.       -- so these are unchecked.  :-(
  100.       unchecked_test("log(0.0)",zero.log.str,"-Inf");
  101.       unchecked_test("log(-0.0)",(-zero).log.str,"NaN");
  102.       unchecked_test("log(-1.0)",(-one).log.str,"NaN");
  103.  
  104.       finish;
  105.    end;
  106. end; -- class TEST_MATH{T}
  107.  
  108. -------------------------------------------------------------------
  109.  
  110. class TEST_FLT is
  111.     main is
  112.         TEST_MATH{FLT}::basic("FLT");
  113.     end;
  114. end;
  115. -------------------------------------------------------------------
  116. class TEST_FLTD is
  117.     main is
  118.     TEST_MATH{FLTD}::basic("FLTD");
  119.     end;
  120. end; -- class MAIN
  121.  
  122. -------------------------------------------------------------------
  123.